package com.qianli.cashmama.product.server.controller;

import com.alibaba.fastjson.JSON;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.collect.Maps;
import com.jsoniter.output.JsonStream;
import com.qianli.cashmama.product.facade.ProductFacade;
import com.qianli.cashmama.product.facade.result.Product;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.qianli.cashmama.product.server.controller.pay.HttpUtil;
import com.qianli.cashmama.product.server.controller.pay.OpenApiResponse;
import com.qianli.cashmama.product.server.controller.pay.PayController;
import com.qianli.cashmama.product.server.entity.DictEntity;
import com.qianli.cashmama.product.server.mapper.DictMapper;
import com.qianli.cashmama.product.server.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
public class OkController {

    private static final Log logger = LogFactory.getLog(OkController.class);

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductFacade productFacade;

    @Value("${server.test}")
    private String testName;

    @RequestMapping(value = "/ok")
    @ResponseBody
    public String ok() {
        logger.info("== ok ==");
        return "ok";
    }

    @RequestMapping(value = "/logok")
    @ResponseBody
    public String logok() {
        List<ProductCategory> allActiveProductCategory = productService.getAllActiveProductCategory();
        logger.info("productlogok-------------------");
        return "cashmama-product,ok," + testName + "," + JSON.toJSONString(allActiveProductCategory);
    }

    @GetMapping("/redisTest")
    @ResponseBody
    public String redisTest() {
        redisClient.setString("KEY_{ID}", "666", "af2324234");
        String value = redisClient.getString("KEY_af2324234");
        redisClient.del("KEY_{ID}", "af2324234");
        return value;
    }

    @GetMapping("/getCategory/{merchantCode}")
    @ResponseBody
    public List<Product> getProduct(@PathVariable("merchantCode") String merchantCode) {
        return productFacade.getProductByMerchantCode(merchantCode).getData();
    }

    @GetMapping("/ok22")
    @ResponseBody
    public List<ProductCategory> getProductCategory() {
        Map headers = Maps.newHashMap();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        Map<String, String> params = Maps.newHashMap();
        params.put("code", "1");
        params.put("mchId", "123456");
        params.put("mchOrderNo", "orderNoFor3d");
        params.put("productId", "11");
        params.put("notifyUrl", "www.baidu.com");
        params.put("orderAmount", "100");
        params.put("payOrderId", "asdfasdfa");
        params.put("paySuccessTime", "1583548355326");
        params.put("message", "message");

        String result = HttpUtil.postPage("http://localhost:9101/pay/callback", params, headers);
        logger.info("== 访问支付链接接口 ==,res:{}", result);
        return productFacade.getAllProductCategory().getData();
    }
}
